package com.mmall.controller.backend;

import com.github.pagehelper.PageInfo;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Skill;
import com.mmall.pojo.User;
import com.mmall.service.ISkillService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @CLASS SkillManageController
 * @DESCRIPTION TODO
 * @AUTHor tang
 * @Date 2019-08-22 16:12
 * @VERSION 1.0
 **/
@Controller
@RequestMapping("/manage/skill")
public class SkillManageController {

    @Autowired
    private ISkillService iSkillService;

    @Autowired
    private IUserService iUserService;

    @RequestMapping(value = "list",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<PageInfo> list(
            HttpSession session,
            String name,
            @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
            @RequestParam(value = "pageNum",defaultValue = "10") int pageSize){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iSkillService.list(user.getId(),name,pageNum,pageSize);
        }
    }

    @RequestMapping(value = "save",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> save(HttpSession session, Skill skill){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iSkillService.save(user,skill);
        }
    }

    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> delete(HttpSession session,Integer skillId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录请重新登录");
        }
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("当前用户没有登录后台权限");
        }else {
            return iSkillService.delete(user.getId(),skillId);
//            return iSkillService.save(user,skill);
        }
    }

}
